package com.qinggeng.project.system.service.impl;

import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import com.google.common.collect.Maps;
import com.qinggeng.common.enums.AppTurnPageEnum;
import com.qinggeng.common.enums.UserTypeEnum;
import com.qinggeng.common.enums.JpushUserEnum;
import com.qinggeng.project.api.utils.MessagePushUtils;
import com.qinggeng.project.system.domain.JPushExtraModel;
import com.qinggeng.project.system.domain.JPushMessageModel;
import com.qinggeng.project.system.domain.Orders;
import com.qinggeng.project.system.domain.UserAlias;
import com.qinggeng.project.system.service.IOrdersService;
import com.qinggeng.project.system.service.IUserAliasService;
import com.qinggeng.project.system.service.MessagePushService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;


@Slf4j
@Service
public class MessagePushServiceImpl implements MessagePushService {

    private static final String CODE="code",ARG="arg",IS_RETURN="isReturn";
    @Autowired
    private IOrdersService ordersService;
    @Autowired
    private IUserAliasService userAliasService;

    @Override
    public void JPushMessage(JPushMessageModel model){
        try{
            //添加推送属性
            HashMap<String, String> resultMap = Maps.newHashMap();
            resultMap.put(CODE, model.getExtraModel().getCode());//界面编码
            resultMap.put(ARG, model.getExtraModel().getArg()); //参数
            resultMap.put(IS_RETURN,model.getExtraModel().getIsReturn());//跳转类型（1=原生界面，0=不跳转，2=h5页面）

            PushPayload pushPayload = MessagePushUtils.push(Audience.alias(model.getUserAlias().getAlias()), model.getMessage(), resultMap);

            if(UserTypeEnum.COURIER.getCode().equals(model.getUserAlias().getUserType())){
                MessagePushUtils.sendPushTryCatch(JpushUserEnum.骑手端, pushPayload);
            }

            if(UserTypeEnum.MEMBER.getCode().equals(model.getUserAlias().getUserType())){
                MessagePushUtils.sendPushTryCatch(JpushUserEnum.客户端, pushPayload);
            }

        }catch (Exception e){
            log.error("发送推送消息失败！"+e.getMessage());
        }
    }

    @Override
    public void JPushMessage(UserAlias userAlias, String message , JPushExtraModel extra){

        JPushMessageModel model=new JPushMessageModel();
        model.setUserAlias(userAlias);
        model.setMessage(message);
        model.setExtraModel(extra);
        this.JPushMessage(model);
    }

    @Override
    public void JPushMessage(UserAlias userAlias, String message, AppTurnPageEnum turnPageEnum, String arg) {
        JPushExtraModel model=new JPushExtraModel();
        model.setCode(turnPageEnum.getCode());
        model.setArg(arg);
        model.setIsReturn(turnPageEnum.getTurnType());
        this.JPushMessage(userAlias,message,model);
    }

    @Override
    public void JPushMessage(UserAlias userAlias, String message,String code, String arg,String isReturn) {
        JPushExtraModel model=new JPushExtraModel();
        model.setCode(code);
        model.setArg(arg);
        model.setIsReturn(isReturn);
        this.JPushMessage(userAlias,message,model);
    }


    @Override
    public void pushMemberOrdersMessage(Orders orders,String message){
        if(null==orders){
            return;
        }
        //根据用户ID查询普通用户的别名
        UserAlias userAlias=userAliasService.selectMemberUserAliasByUserId(orders.getUserId());
        try{
            //消息推送
            JPushExtraModel extra=new JPushExtraModel(AppTurnPageEnum.ORDERS_INFO,orders.getOrdersId().toString());
            this.JPushMessage(userAlias,message,extra);

        }catch (Exception e){
            log.error(e.getMessage());
        }
    }

    @Override
    public void pushCourierOrdersMessage(Orders orders,String message){
        if(null==orders){
            return;
        }
        //根据订单ID查询骑手别名列表
        List<UserAlias> userAliasList=userAliasService.selectCourierUserAliasListByOrdersId(orders.getOrdersId());
        if(CollectionUtils.isEmpty(userAliasList)){
            return;
        }
        try{
            //批量消息推送
            for (UserAlias userAlias :userAliasList) {
                JPushExtraModel extra=new JPushExtraModel(AppTurnPageEnum.ORDERS_INFO,orders.getOrdersId().toString());
                this.JPushMessage(userAlias,message,extra);
            }
        }catch (Exception e){
            log.error(e.getMessage());
        }
    }

}
